<template>
  <div class="run-modal-style" style="height:460px">
    <!-- 搜索区域 -->
    <SelfAdaption>
      <Form
        :model="searchForm"
        ref="roleSearchForm"
        slot="adaption-bar"
        label-position="top"
        class="ivu-form-no-margin-bottom"
        inline
      >
        <!-- 角色编码 -->
        <Form-item prop="roleCode" :label="$t('module.role.roleCode')">
          <Input
            type="text"
            :maxlength="30"
            v-model="searchForm.roleCode"
            :placeholder="$t('module.role.warning.enterRoleCode')"
            
            @on-enter="search"
          />
        </Form-item>
        <!-- 角色名称 -->
        <Form-item prop="roleName" :label="$t('module.role.roleName')">
          <Input
            type="text"
            :maxlength="30"
            v-model="searchForm.roleName"
            :placeholder="$t('module.role.warning.enterRoleName')"
            
            @on-enter="search"
          />
        </Form-item>

        <!-- 查询\重置按钮 -->
        <Form-item prop="button" class="search-solt-btn">
          <RPButton optType="search" @click="search" />
          <RPButton optType="reset" @click="reset" />
        </Form-item>
      </Form>
    </SelfAdaption>
    <InnerPageTable ref="roleTable" :config="tableConfig">
      <!-- handler bar 查询/重置-->
      <!--
      <div slot="handle-bar">
        <RPButton optType="search" @click="search" />
        <RPButton optType="reset" @click="reset" />
      </div>
      -->
    </InnerPageTable>
  </div>
</template>
<script>
import orgAssignRoleAPI from "@/api/orgAssignRoleAPI.js";
import { orgSelectRoleColumns } from "./columns.js";
import locales from "./locales.js";

export default {
  name: "org-select-role",
  mixins: [locales],
  props: {
    tenantCode: {
      type: String,
      default() {
        return "";
      }
    },
    orgId: {
      type: String,
      default() {
        return "";
      }
    },
    orgPath: {
      type: String,
      default() {
        return "";
      }
    }
  },
  data() {
    return {
      searchForm: {
        orgId: this.orgId,
        tenantCode: this.tenantCode,
        roleType: 1,
        enabled: 1,
        roleName: "",
        roleCode: ""
      },
      actions: [
        {
          text: this.$t("common.save"),
          theme: "primary",
          handle: () => {
            this.save();
          },
          loading: false
        },
        {
          text: this.$t("common.close"),
          handle: () => {
            this.$emit("Close");
          }
        }
      ]
    };
  },
  computed: {
    tableConfig() {
      return {
        loadDataFn: ({ responseProcess, param }) => {
          orgAssignRoleAPI.queryUnassignedRole(param).then(data => responseProcess(data));
        },
        initParam: this.searchForm,
        autoFirst: true,
        columnsFn: orgSelectRoleColumns.bind(this)
      };
    }
  },
  created: function() {
    this.$emit("SetTitle", this.$t("module.role.selectRoles"));
    this.$emit("SetPageWidth", 860);
    this.$emit("SetPageActions", this.actions);
  },
  mounted() {},
  methods: {
    search(page = 1) {
      this.$refs["roleTable"].load(this.searchForm, page);
    },
    reset() {
      this.$refs["roleSearchForm"].resetFields();
      this.search(1);
    },
    save() {
      // 如果未勾选角色，无需保存
      let selectedIds = this.getSelectedIds();
      if (selectedIds.length == 0) {
        this.$message.error({
          content: this.$t("module.role.warning.selectRole"),
          duration: 3
        });
        return;
      }
      let param = {
        orgId: this.orgId,
        orgPath: this.orgPath,
        roleId: selectedIds
      };
      this.actions[0].loading = true;
      orgAssignRoleAPI.saveAssignedRole(param).then(data => {
        this.$message.success({
          content: this.$t("module.role.warning.assignRoleSuccessfully"),
          duration: 3
        });
        this.$emit("Close", true);
      });
    },
    getSelectedIds() {
      let list = this.$refs["roleTable"].getSelectioned() || [];
      let ids = [];
      for (let row of list) {
        ids.push(row.id);
      }
      return ids;
    }
  }
};
</script>

<style lang="less">
.run-modal-style {
  .single-table-con {
    padding-top: 0;
    .table-bar {
      display: none;
    }
  }
}
</style>